package com.ycg.vue.utils;

import com.ycg.vue.constant.EmailConstant;
import com.ycg.vue.modules.system.Entity.EmailEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 定时任务工具类
 * @Author jink
 * @Date 2024/8/28
 */
@Slf4j
@Component
public class SpringTaskUtils {
    @Resource
    private EmailUtils emailUtils;

    @Resource
    private WeatherUtils weatherUtils;

    //正文内容
    public static String message = "";
    //收件人
    public static String to = "";
    //标题
    public static String title = "";

    public void showLog() {
        log.info("邮箱发送：收件人：{}，标题：{}，正文：{} ", to, title, message);
    }

    @Async
//    @Scheduled(cron = "5 0 9 * * ?")
    public void printLog() {
        log.info("生日提醒定时任务执行了");
        LocalDate birthday = LocalDate.parse("2024-09-07");
        LocalDate today = LocalDate.now();
        List<String> toList = new ArrayList<>();
        toList.add("3360976193@qq.com");
        toList.add("2410872915@qq.com");
        title = "生日提醒";
        long time = ChronoUnit.DAYS.between(birthday, today);
        if (time > 0) return;
        time = time < 0 ? time * -1 : time;
        message = "今天是" + today.getYear() + "年" + today.getMonthValue() + "月" + today.getDayOfMonth() + "日，距离您宇宙第一好的好朋友生日还有: " + time + "天哦！（此消息为自动提醒，请勿回复）";
        toList.forEach(item -> {
            emailUtils.sent(EmailEntity.builder().to(item).subject(title).content(message).build());
            showLog();
        });
    }

    @Async
    @Scheduled(cron = "0 0 8 * * ?")
    public void sentWeather() {
        log.info("天气提醒定时任务执行了");
        message = weatherUtils.getWeatherByString("shijiazhuang");
        to = "2210841377@qq.com";
        title = weatherUtils.getTitle("shijiazhuang");
        emailUtils.sent(EmailEntity.builder().to(to).subject(title).content(message).build());
        showLog();
    }

    @Async
//    @Scheduled(cron = "10 * 25 * * ?")
    public void sentWeather2() {
        log.info("测试邮箱发送了");
        String body = "<h1>测试</h1><div class=\"text\">这是一个测试邮件</div>";
        EmailConstant.body(body);
        String style = ".text{color:red;font-size:20px}";
        EmailConstant.style(style);
        message = EmailConstant.text();
        to = "2196359343@qq.com";
        title = "html测试";
        emailUtils.sent(EmailEntity.builder().to(to).subject(title).content(message).build(),true);
        showLog();
    }
}
